package com.zmn.plat.services.impl.product.categorybrand;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.product.categorybrand.CategoryBrand;
import com.zmn.plat.model.entity.product.categorybrand.CategoryBrandQuery;
import com.zmn.plat.model.vo.product.categorybrand.CategoryBaseBrandVo;
import com.zmn.plat.model.vo.product.categorybrand.CategoryBrandVo;
import com.zmn.plat.persistence.interfaces.product.categorybrand.CategoryBrandDAO;
import com.zmn.plat.services.interfaces.product.categorybrand.CategoryBrandService;

import lombok.extern.slf4j.Slf4j;

/**
 * 描述: 产品分类开通品牌表基础接口实现类
 *
 * @author tanghongjun
 * @since 2020-08-19 11:18:36
 **/
@Slf4j
@Service
public class CategoryBrandServiceImpl implements CategoryBrandService {

    private static final String TAG = "产品分类开通品牌表基础服务接口";

    @Resource
    private CategoryBrandDAO categoryBrandDao;

    /**
     * 描述: 根据id查询数据
     *
     * @param relateId
     *            id
     * @author tanghongjun
     * @since 2020-08-19 11:18:36
     **/
    @Override
    public CategoryBrand findById(Integer relateId) {

        if (Objects.isNull(relateId)) {
            log.error("[{}],根据id查询数据relateId为null", TAG);
            return null;
        }
        return categoryBrandDao.findById(relateId);
    }

    /**
     * 描述: 按条件查询数据 支持分页
     *
     * @param query
     *            查询对象
     * @author tanghongjun
     * @since 2020-08-19 11:18:36
     **/
    @Override
    public List<CategoryBrandVo> listPageByQuery(CategoryBrandQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        query.setTotalCount(countByQuery(query));
        return categoryBrandDao.listPageByQuery(query);
    }

    /**
     * 描述: 按条件查询数据 不支持分页
     *
     * @param query
     *            查询对象
     * @author tanghongjun
     * @since 2020-08-19 11:18:36
     **/
    @Override
    public List<CategoryBrand> listByQuery(CategoryBrandQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return categoryBrandDao.listByQuery(query);
    }

    @Override
    public List<CategoryBaseBrandVo> listByCategIdsAndStatus(List<Integer> categList, Integer status) {
        if (CollectionUtils.isEmpty(categList)) {
            log.error("[{}],按条件查询数据,categList为null", TAG);
            return Collections.EMPTY_LIST;
        }
        return categoryBrandDao.listByCategIdsAndStatus(categList, status);
    }

    /**
     * 描述: 按条件查询数据 不支持分页
     *
     * @param query
     *            查询对象
     * @author tanghongjun
     * @since 2020-08-19 11:18:36
     **/
    @Override
    public Integer countByQuery(CategoryBrandQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return categoryBrandDao.countByQuery(query);
    }

    /**
     * 描述: 根据id修改数据 id为必传
     *
     * @param categoryBrand
     *            待修改数据
     * @return java.lang.Integer
     * @author tanghongjun
     * @since 2020-08-19 11:18:36
     **/
    @Override
    public Integer updateByKey(CategoryBrand categoryBrand) {

        if (Objects.isNull(categoryBrand.getRelateId())) {
            log.error("[{}],根据id修改数据,categoryBrand为null", TAG);
            return null;
        }
        return categoryBrandDao.updateByKey(categoryBrand);
    }

    @Override
    public Integer updateSortByKey(Integer id, Integer sort, String mcStaffRealName) {
        CategoryBrand brand = new CategoryBrand();
        brand.setRelateId(id);
        brand.setSort(sort);
        brand.setUpdater(mcStaffRealName);
        brand.setUpdateTime(new Date());
        return updateByKey(brand);
    }

    @Override
    public Integer updateStatusBatchByKey(List<Integer> arrayIds, Integer status, String mcStaffRealName) {
        return categoryBrandDao.updateStatusBatchByKey(arrayIds, status, mcStaffRealName);
    }

    /**
     * 描述: 新增数据
     *
     * @param categoryBrand
     *            待新增数据
     * @return java.lang.Integer 主键key
     * @author tanghongjun
     * @since 2020-08-19 11:18:36
     **/
    @Override
    public Integer insert(CategoryBrand categoryBrand) {

        if (Objects.isNull(categoryBrand)) {
            log.error("[{}],新增数据,categoryBrand为null", TAG);
            return null;
        }
        return categoryBrandDao.insert(categoryBrand);
    }

    @Override
    public Integer insertBatch(List<CategoryBrand> categoryBrands, String mcStaff) {
        if (!CollectionUtils.isEmpty(categoryBrands)) {
            final String operator = StringUtil.isBlank(mcStaff) ? "SYSTEM" : mcStaff;
            Date now = DateUtil.getNow();
            categoryBrands.stream().forEach(e -> {
                e.setCreater(operator);
                e.setUpdater(operator);
                e.setCreateTime(now);
                e.setUpdateTime(now);
            });
            return categoryBrandDao.insertBatch(categoryBrands);
        }
        return null;
    }

    /**
     * 描述: 根据主键删除数据
     *
     * @param relateId
     * @return: 执行结果码
     * @auther: tanghongjun
     * @since: 2020-08-19 11:18:36
     */
    @Override
    public Integer deleteByKey(Integer relateId) {

        if (Objects.isNull(relateId)) {
            log.error("[{}],根据id删除数据,relateId为null", TAG);
            return null;
        }
        return categoryBrandDao.deleteByKey(relateId);
    }

}